//回单管理详情（整车）

interface IReceipManagentOrderViewModel extends ng.IScope{
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 发货单详情
     */
    orderData:invoiceManagement;
    /**
     * 订单总单详情
     */
    OData:invoiceManagementOrder;
    /**
     * 预付款
     */
    advancePaymentData:Array<advancePaymentVM>;
    /**
     * 预付款1
     */
    advancePriceOne:string;
    /**
     * 预付款方式1
     */
    advancePriceTypeOne:string;
    /**
     * 预付款2
     */
    advancePriceTwo:string;
    /**
     * 预付款方式2
     */
    advancePriceTypeTwo:string;
    /**
     * 总单列表
     */
    orderTotalData:Array<invoiceManagementOrder>;
    /**
     * 子单详情
     */
    orderChildData:Array<invoiceManagementOrderChild>;
    /**
     * 子单数据
     */
    childData:invoiceManagementOrderChild;
    /**
     * 合同信息
     */
    contractData:invoiceManagementContract;
    /**
     * 油卡状态
     */
    oilCardStatus:string;
    /**
     * 合同状态
     */
    contractStatus:string;
    /**
     * 费用信息列表
     */
    orderCostList:Array<invoiceManagementFee>;
    /**
     * 费用数据
     */
    feeData:invoiceManagementFee;
    /**
     * 油卡状态下拉
     */
    oilCardStatusDropDown:ValueListItemResponse[];
    /**
     * 合同状态下拉
     */ 
    contractStatusDropDown:ValueListItemResponse[];
    /**
     * 回单状态下拉
     */
    receiptStatusDropDown:ValueListItemResponse[];
    /**
     * 查看总单详情事件
     */
    getOrderDetail:(item:invoiceManagementOrder) => void;
    /**
     * 子单查看详情事件
     */
    getOrderChild:(item:invoiceManagementOrderChild) => void;
    /**
     * 查看付款详情
     */
    getFeeDetail:(item:invoiceManagementFee)=>void;
    /**
     * 确定事件(修改合同状态,回单状态)
     */
    saveReceipts: (id:string) => void;
    /**
     * 取消事件
     */
    backUrl:(id:string) => void;
    /**
     * 是否可修改备注信息(false为可修改)
     */
    isCanUpdateRemarks:boolean;
    /**
     * 回单状态
     */
    receiptStatus:string;
    /**
     * 回单时间
     */
    receiptTime:string;
    /**
     * 回单附件
     */
    receiptInfoList:any;



    //11.1新增
    /**
     * 结算单据状态
     */
    receiptBillStatus:string;
    /**
     * 结算单据状态下拉
     */
    receiptBillStatusDropDown:ValueListItemResponse[];

    /**
     * 点击图片时候触发，用以旋转放大图片
     */
    handleImgClick:(item:any)=>void;
    /**
     * 用以旋转放大的图片的路径
     */
    selectedImgSrc:string;

}

class ReceiptManagentOrderController{
    /**
     * 承运单id
     */
    id:string;

    /**
     * 附件
     */
    uploaderList: ReceiptAttachment[] = [];

    /**
     * 回单附件地址
     */
    baseImageUrl: string;
    imgList = [];
    constructor(private $scope:IReceipManagentOrderViewModel,private feeService:IFeeService,private valueService:IValueService,private orderService:IorderService ,
        private $location: ng.ILocationService,private routeService: routeService,private $ngBootbox: BootboxService, 
        private carService: ICarService, private $http: ng.IHttpService,private $state: angular.ui.IStateService,private commonService:ICommonService
        ,private ossUploaderService:ossUploaderService){
        this.init();
        
        this.baseImageUrl = this.routeService.getBaseUrl() + "attachment/";
    }
    
    init():void{
        this.id = this.$location.search().carrierOrderId;

        this.$scope.isCanUpdateRemarks = false;
        this.loadData();

        this.loadStatusDropDown(); 

        //默认  结算单据状态是   “无结算单据” 
        this.$scope.receiptBillStatus ="1" ;


        this.$scope.getOrderDetail = this.getOrderDetail;
        this.$scope.getOrderChild = this.getOrderChild;
        this.$scope.getFeeDetail= this.getFeeDetail;
        this.$scope.saveReceipts = this.saveReceipts;
        this.$scope.backUrl = this.backUrl;

        $('#openreceiptTime').click(() => {
		    $('#receiptTime').datetimepicker('show');
		});
		$('#opencontractTime').click(() => {
		    $('#receiptManagent_order_contractTime').datetimepicker('show');
        });
        
        this.$scope.handleImgClick=(item)=>{this.$scope.selectedImgSrc=item.imgSrc;}
    }

    loadData = () => {
        this.orderService.invoiceManagementDetail(this.id).then((res)=>{
            this.$scope.orderData = res.data;
            this.$scope.OData = res.data.orderList[0];
            this.$scope.advancePaymentData = res.data.advancePaymentList;
            this.$scope.orderTotalData = res.data.orderList;
            this.$scope.orderChildData = res.data.orderList[0].childList;
            this.$scope.contractData = res.data.contract;
            this.$scope.oilCardStatus = res.data.contract.oilCardStatusEnum;
            /**
             * 合同详情接口
             */
            this.feeService.contractDetail(this.$scope.contractData.id).then((result) => {  
                this.$scope.contractStatus = result.data.contractStatus;   
            }); 
            this.$scope.orderCostList = res.data.feeList;

            /**
             * 预付款
             */
            if(res.data.advancePaymentList.length>0){
                this.$scope.advancePriceOne = res.data.advancePaymentList[0].advancePrice;
                this.$scope.advancePriceTypeOne = res.data.advancePaymentList[0].advancePriceTypeStr;
                this.$scope.advancePriceTwo = "";
                this.$scope.advancePriceTypeTwo = "";
            }
            if(res.data.advancePaymentList.length>1){
                this.$scope.advancePriceOne = res.data.advancePaymentList[0].advancePrice;
                this.$scope.advancePriceTypeOne = res.data.advancePaymentList[0].advancePriceTypeStr;
                this.$scope.advancePriceTwo = res.data.advancePaymentList[1].advancePrice;
                this.$scope.advancePriceTypeTwo = res.data.advancePaymentList[1].advancePriceTypeStr;
            }

            //回单状态
            this.$scope.receiptStatus = res.data.orderList[0].receiptStatusEnum;
            //11.1 新增
            // //结算单据状态
            if(res.data.orderList[0].settlementStatusEnum=="0"){
                this.$scope.receiptBillStatus = "1"
            }else{
                this.$scope.receiptBillStatus = res.data.orderList[0].settlementStatusEnum;
            }

            //回单签收时间
            this.$scope.receiptTime = res.data.orderList[0].receiptTime;
            //回单附件
            this.$scope.receiptInfoList=[];
            const OssFile = this.ossUploaderService.getOssFile();


            this.$scope.orderChildData.forEach((item,index)=>{

                var itemId = item.id;
                this.$scope.receiptInfoList.push({
                    title:'回单'+(index+1),
                    uploader:null,
                    uploaderName:'receipt'+(index+1)+'Uploader'
                })

                this.$scope.receiptInfoList[index].uploader=new  OssFile(this.$scope,this.$scope.receiptInfoList[index].uploader,{uploaderList:this.uploaderList,tag:index+1,multiple:true});
                this.$scope.receiptInfoList[index].uploader.onSuccess=(item=>{
                    this.onSuccessItem(itemId,item.fileName, item.imgSrc,0,item._file);
                        if (this.$scope.receiptTime == "" || this.$scope.receiptTime == null || this.$scope.receiptTime == "-") {
                            this.$scope.receiptTime = String(this.commonService.transformTime(new Date(), "yyyy-MM-dd HH:mm:ss"));
                        }
                })
              

                let receiptUploaderArr= item.receiptAttachments;
                let imgList = [];
                receiptUploaderArr.forEach((itemR,indexR)=>{
                    //加载并显示图片
                    let urls = this.baseImageUrl + itemR.attachmentId;
                    this.uploaderList.push({
                        orderChildId:item.id,
                        name:itemR.attachmentName,
                        path:itemR.attachmentPath,
                        creationTime :itemR.attachmentCreationTime
                    });
                    // this.commonService.showFileItem(itemR,this.$scope[attachName]);   
                    this.$scope.receiptInfoList[index].uploader.addByAttach(itemR)
                });
                //删除图片
                // this.$scope[attachName].onDelete = (item) =>{this.commonService.onDelete(item,this.uploaderList)}; 
                // this.$scope.receiptInfoList.push({ imgName:attachName, Name:this.$scope[attachName],childerId:item.id });
            });
    });  
}

   


    /**
     * 状态下拉
     */
    loadStatusDropDown():void{
        this.$scope.receiptStatusDropDown = this.valueService.getReceiptStatusList().data;
        this.$scope.contractStatusDropDown = this.valueService.getContractStatusList().data.slice(1,3);
        this.$scope.oilCardStatusDropDown = this.valueService.getOilCCardStatusList().data;
        //11.1新增  结算单据状态
        this.$scope.receiptBillStatusDropDown = this.valueService.getReceiptBillStatusDropDown().data;
    }

    /**
     * 查看总单详情事件
     */
    getOrderDetail = (item:invoiceManagementOrder) =>{
        this.$scope.OData = item ;
    }

    /**
     * 查看子单详情事件
     */
    getOrderChild = (item:invoiceManagementOrderChild) => {
        this.$scope.childData = item;
    }

    /**
     * 查看付款详情
     */
    getFeeDetail=(item:invoiceManagementFee)=>{
        this.$scope.feeData = item;
    }

    /**
     * 选择上传文件后
     */
    onSuccessItem = (childId:string,fileName: string, filePath: string,tag?:number,file?:any) => {
        
        let attachment = {
            orderChildId: childId,
            name: fileName,
            path: filePath,
            attachmentTag: "",
            creationTime: ''
        };
        this.uploaderList.push(attachment);
    }

    /**
     * 修改合同和回单状态
     */
    saveReceipts = (id:string) => {
        if (this.$scope.receiptTime == "" || this.$scope.receiptTime == null || this.$scope.receiptTime == "-") {
            if (this.$scope.receiptStatus != "" && this.$scope.receiptStatus != "1" && this.$scope.receiptStatus != null) {
                this.$ngBootbox.alert("请填写回单时间");
                return;
            }
        } else if (this.$scope.contractStatus == "" || this.$scope.contractStatus == null) {
            this.$ngBootbox.alert("请选择合同状态");
            return;
        } else if (this.$scope.oilCardStatus == "" || this.$scope.oilCardStatus == null) {
            this.$ngBootbox.alert("请选择油卡状态");
            return;
        }else if(this.$scope.receiptBillStatus==null || this.$scope.receiptBillStatus=="" || this.$scope.receiptBillStatus == "0"){
            this.$ngBootbox.alert("请选择结算单据状态");
            return;
        }

        this.$ngBootbox.confirm('确定修改回单、结算单据和合同状态吗?').then(() => {
            this.feeService.receipts(
                false,
                this.$scope.OData.id,
                this.$scope.receiptStatus,
                this.$scope.receiptTime,
                this.uploaderList,
                this.$scope.receiptBillStatus
                ).then((res)=> {
                    this.$state.go("app.fee.receiptManagentList");
                },(rej)=> {  });
            this.feeService.changeContractStatus(
                this.$scope.contractData.id,
                this.$scope.oilCardStatus,
                this.$scope.contractStatus
                ).then((res)=> {},(rej) => { });
            this.feeService.carrierOrderRemarks(
                this.id,
                this.$scope.orderData.remarks
            ).then((res) => {
            },(rej) => { });
        },() => { return; });
    }

    /**
     * 取消事件
     */
    backUrl = (id:string) => {
        this.$state.go("app.fee.receiptManagentList",{ name: 'receiptManagent' });
    }
    
}
angular.module("tms").controller("receiptManagentOrderController",["$scope","feeService","valueService","orderService",
"$location","routeService","$ngBootbox","carService",
"$http","$state","commonService","ossUploaderService",ReceiptManagentOrderController]);